home *** CD-ROM | disk | FTP | other *** search
- /* string search in string as if standerd C library */
- /* (c) Shinwa */
- /* release note: */
- /* 1992/12/8 first release */
- /* Permission to use, copy, distribute, this software is garanted */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- #define ON (1)
- #define OFF (0)
-
- char *Myname; /* This command name set in main() */
-
- struct { /* command line options */
- int o : 1;
- int t : 1;
- int v : 1;
- } Option = {OFF, ON, OFF}; /* default option */
-
- void help(void)
- {
- if (Option.v) {
- puts(" find string in source and return error code. ver 1.0 93/2/26");
- }
- printf("usage %s [-otvh] source string\n", Myname);
- if (Option.v) {
- puts(" -o, -t select case of string Two or One case");
- puts(" -h print this message");
- puts(" -v message more wordly");
- puts(" by Shiwa");
- puts(" Permission to use, copy, distribute, this software is garanted");
- }
- }
-
- char *tolower_str(char *s)
- {
- char *p;
-
- for (p=s; *p != '\0'; p++)
- *p = tolower(*p);
- return s;
- }
-
-
- int main(int argc, char **argv)
- {
- char *source; /* source string */
- char *string; /* find string in source */
- char *p; /* pointer to string in source */
- int i, j;
-
- Myname = argv[0];
- source = string = NULL; /* check whether set or not */
- for (i=1; i < argc; i++) {
- if (argv[i][0] == '-' || argv[i][0] == '/') {
- for (j=1; argv[i][j] != '\0'; j++) {
- if (argv[i][j] == 'o') {
- Option.o = ON;
- Option.t = OFF;
- } else if (argv[i][j] == 't') {
- Option.t = ON;
- Option.o = OFF;
- } else if (argv[i][j] == 'v') {
- Option.v = ON;
- } else if (argv[i][j] == 'h' || argv[i][j] == '?') {
- Option.v = ON;
- help();
- exit(EXIT_SUCCESS);
- } else {
- fprintf(stderr, "%s: Option error!\n", Myname);
- help();
- exit(EXIT_FAILURE);
- }
- }
- } else if (source == NULL) {
- source = argv[i];
- } else if (string == NULL) {
- string = argv[i];
- } else {
- fprintf(stderr, "%s: Too many option strings!\n", Myname);
- help();
- exit(EXIT_FAILURE);
- }
- }
-
- if (string == NULL) {
- fprintf(stderr, "%s: Too few option string!\n", Myname);
- help();
- exit(EXIT_FAILURE);
- }
- if (Option.v) {
- printf("find string with %s\n", (Option.t == ON) ? "Two case" : "One case ");
- }
- if (Option.o) {
- tolower_str(source);
- tolower_str(string);
- }
-
- p = strstr(source, string);
-
-
- if (p) {
- if (Option.v) {
- printf(" found %s in %s at %d\n",
- string, source, (int) (p - source + 1));
- }
- return (int) (p - source + 1);
- } else {
- if (Option.v) {
- printf(" not found %s in %s\n", string, source);
- }
- return 0;
- }
- }
-
-
-